package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    LinearLayout message, friend, book, settings;

    ImageView wxImage, friendImage, bookImage, settingsImage;

    Fragment mTab1 = new wxFragment();
    Fragment mTab2 = new friendFragement();
    Fragment mTab3 = new bookFragment();
    Fragment mTab4 = new settingsFragment();

    FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        initView(); //关联对象
        initEvent(); //设置监听器
        initFragment(); //添加界面
        selectTab(1);
    }

    private void initView() {
        message = findViewById(R.id.message);
        friend = findViewById(R.id.friend);
        book = findViewById(R.id.book);
        settings = findViewById(R.id.settings);

        wxImage = findViewById(R.id.wxImage);
        friendImage = findViewById(R.id.friendImage);
        bookImage = findViewById(R.id.bookImage);
        settingsImage = findViewById(R.id.settingsImage);
    }

    private void initEvent() {
        message.setOnClickListener(this);
        friend.setOnClickListener(this);
        book.setOnClickListener(this);
        settings.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        resetImg(); //还原图标

        switch (v.getId()){
            case R.id.message:
                selectTab(1);
                break;
            case R.id.friend:
                selectTab(2);
                break;
            case R.id.book:
                selectTab(3);
                break;
            case R.id.settings:
                selectTab(4);
                break;
        }
    }

    private void resetImg() {
        wxImage.setImageResource(R.drawable.tab_weixin_normal);
        friendImage.setImageResource(R.drawable.tab_find_frd_normal);
        bookImage.setImageResource(R.drawable.tab_address_normal);
        settingsImage.setImageResource(R.drawable.tab_settings_normal);
    }

    private void selectTab(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction); //隐藏全部界面
        switch (i){
            case 1:
                transaction.show(mTab1);
                wxImage.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 2:
                transaction.show(mTab2);
                friendImage.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 3:
                transaction.show(mTab3);
                bookImage.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 4:
                transaction.show(mTab4);
                settingsImage.setImageResource(R.drawable.tab_settings_pressed);
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(mTab1);
        transaction.hide(mTab2);
        transaction.hide(mTab3);
        transaction.hide(mTab4);
    }

    private void initFragment(){
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.content, mTab1);
        transaction.add(R.id.content, mTab2);
        transaction.add(R.id.content, mTab3);
        transaction.add(R.id.content, mTab4);
        transaction.commit();
    }

}